package Work;

public class Test2 {
    public static void main(String[] args) {
        int[] array = {1,2,3,4,5,6,7,8,9};
        int k = 2;
        int left = 0;
        int right = array.length-1;
        int ret = binarySearch(array,k,left,right);
        System.out.println(ret);
    }
    public static int binarySearch(int[] array, int k, int left, int right) {

        while(left <= right) {
            int mid = left + (right - left)/2;
            if(array[mid] < k) {
                left = mid + 1;
            }
            else if(array[mid] > k) {
                right = mid - 1;
            }
            else if(array[mid] == k) {
               return mid;
            }
        }
//        if(left > right) {
//            return -1;
//        }
        return -1;
    }
}
